package com.example.enterprisemanager.mvvm.fragment.allocate;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.mvvm.adapter.AllocateAdapter;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.allocate.ben.SendEnrollGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.EnrollGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;
import com.example.enterprisemanager.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class AllocateViewModel extends BaseViewModel<AllocateModel> {
    public AllocateAdapter adapter;
    public MutableLiveData<SendEnrollGoods> sendEnrollMutableLiveData;

    public AllocateViewModel(@NonNull Application application) {
        super(application, new AllocateModel());
        init();
    }

    private void init() {
        adapter = new AllocateAdapter();
        sendEnrollMutableLiveData = new MutableLiveData<>();

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Logger.d("onItemChildClick" + position);

                if (!BmobUser.isLogin() || BmobUser.getCurrentUser(User.class).getPower() > 3) {
                    ToastUtil.show(getApplication(), "您的权限不足，需要的权限为3及以上");
                    return;
                }
                EnrollGoods enrollGoods = (EnrollGoods) adapter.getItem(position);
                sendEnrollMutableLiveData.setValue(new SendEnrollGoods(position, enrollGoods));
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mModel.getCanBeAllocated(new Consumer<List<EnrollGoods>>() {
            @Override
            public void accept(List<EnrollGoods> enrollGoods) throws Exception {
                adapter.setNewData(enrollGoods);
            }
        }, new Consumer<Exception>() {
            @Override
            public void accept(Exception e) throws Exception {

            }
        });
    }


    public void haveEnroll(int num, String department, int position, EnrollGoods enrollGoods) {

        List<String> ids = enrollGoods.getGoodsIds();
        List<String> haveAllocateIds = new ArrayList<>();
        if (num > ids.size()) {
            ToastUtil.show(getApplication(), "无法分配多于已登记物品的数量");
            return;
        }

        List<BmobObject> goods = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Goods good = new Goods();
            String id = ids.get(0);
            good.setObjectId(id);
            good.setDepartment(department);
            goods.add(good);
            haveAllocateIds.add(id);
            ids.remove(0);
        }

        enrollGoods.setNumber(enrollGoods.getNumber() - num);
        enrollGoods.setHaveAllocateNum(enrollGoods.getHaveAllocateNum() + num);
        enrollGoods.setGoodsIds(ids);

        mModel.haveAllocateUpdateNum(goods, enrollGoods, (results) -> {
            ToastUtil.show(getApplication(), "分配成功");
            adapter.notifyItemChanged(position);
        }, (e) -> {
            ToastUtil.show(getApplication(), "分配失败");
        });

        String userName = BmobUser.getCurrentUser(User.class).getUsername();

        mModel.haveAllocate(enrollGoods, num, haveAllocateIds, department, userName, () -> {
            Logger.d("登记信息存储成功");
        }, (e) -> {
            Logger.d(e.getMessage());
        });

    }
}
